#-*- coding:utf-8 -*-  
import requests,json,re,csv,time

session = requests.Session()
get_url = 'http://www.fund123.cn/fund'
TXTtemp = session.get(get_url,verify=False)
txtbuffer=str(TXTtemp.text)
strlist1=txtbuffer.replace("\n"," ")
findword =u"(<script>window.context = .*?;</script>)"
pattern_word = re.compile(findword)
results_word = pattern_word.findall(strlist1)
results = results_word[0].replace('<script>window.context = ',' ').replace(';</script>',' ')
json_results = json.loads(results)
print(session.cookies['ALIPAYJSESSIONID'])
print(session.cookies['ctoken'])
print(json_results['csrf'])
scrf = str(json_results['csrf'])
post_url= 'http://www.fund123.cn/api/fund/queryFundList?_csrf=' + scrf
cookies = 'ctoken=' + str(session.cookies['ctoken']) +'; ALIPAYJSESSIONID='+ str(session.cookies['ALIPAYJSESSIONID'])
header = {
    'Host': 'www.fund123.cn',
    'Connection': 'keep-alive',
    'Content-Length': '13',
    'accept': 'json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
    'Content-Type': 'application/json',
    'Origin': 'http://www.fund123.cn',
    'Referer': 'http://www.fund123.cn/fund',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cookie': cookies
}

csv_header = ['int','基金key', '基金代码', '基金名称','单位净值','日增长率']
data_csv = []
data_jijin_int = 1
pageNum_int = 1
while pageNum_int < 257:
    payload = "{\"pageNum\":"+str(pageNum_int) +"}"
    response=session.post(post_url,headers=header,data=payload,verify=False)
    print(response.text)
    time.sleep(1)
    json_results = json.loads(str(response.text))
    data_int1 = 0
    while data_int1 < 20:
        data2 = []
        data2.append(data_jijin_int)
        data2.append(json_results['valuedatalist']['listData'][data_int1]['key'])
        data2.append(json_results['valuedatalist']['listData'][data_int1]['fundCode'])
        data2.append(json_results['valuedatalist']['listData'][data_int1]['fundName'])
        data2.append(json_results['valuedatalist']['listData'][data_int1]['netValue'])
        data2.append(json_results['valuedatalist']['listData'][data_int1]['dayOfGrowth'])
        data_csv.append(data2)
        data_jijin_int = data_jijin_int + 1
        data_int1 = data_int1 + 1



    pageNum_int = pageNum_int + 1


datetime = time.strftime("%Y-%m-%d", time.localtime())
namedatetime = datetime+'.csv'
with open(namedatetime, 'w', encoding='utf-8-sig', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(csv_header)
    writer.writerows(data_csv)